package com.boot.exception;


import com.boot.pojo.Result;
import com.boot.service.ex.*;
import com.boot.util.Constants;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

//全局异常捕获
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public Result handle(Exception se){
        if(se instanceof NotFindUserException){
            return new Result(Constants.CODE_5001,se.getMessage());
        }else if(se instanceof NotOnlyOneException){
            return new Result(Constants.CODE_5001,se.getMessage());
        }else if(se instanceof UsernameExit){
            return new Result(Constants.CODE_5002,se.getMessage());
        }else if(se instanceof WithoutData){
            return new Result(Constants.CODE_5003,se.getMessage());
        }else if(se instanceof WithoutToken){
            return new Result(Constants.CODE_5004,se.getMessage());
        }else if(se instanceof TokenNotExitException){
            return new Result(Constants.CODE_5005,se.getMessage());
        }
        else{
            return new Result(Constants.CODE_400,se.getMessage());
        }
    }

}
